package backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by JiaJia on 2019/6/3 0003.
 */
public class LetterCombinations {
    private static final String[] KEYS = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs",
            "tuv", "wxyz"};
    private static List<String> letterCombinations(String digits) {
        List<String> combinations = new ArrayList<>();
        if (digits == null || digits.length() == 0) {
            return combinations;
        } doCombination(new StringBuilder(), combinations, digits);
        return combinations;
    }
    private static void doCombination(StringBuilder prefix, List<String> combinations, final String
            digits) {
        if (prefix.length() == digits.length()) {
            combinations.add(prefix.toString());
            return;
        }
        int curDigits = digits.charAt(prefix.length()) - '0';
        String letters = KEYS[curDigits];
        for (char c : letters.toCharArray()) {
            prefix.append(c); // 添加
            doCombination(prefix, combinations, digits);
            prefix.deleteCharAt(prefix.length() - 1); // 删除
        }
    }

    public static void main(String[] args) {
        String digits="234";
        System.out.println(letterCombinations(digits));
    }
}